/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.view.component;

import java.beans.PropertyChangeListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import org.opu.vcforms.resource.ResourceHandle.IconType;

/**
 *
 * @author yura
 */
public class ButtonProperties extends PropertyListenerImpl 
        implements SwingConstants{
    /** Button properties label */
    public static final String BUTTON = "button";
    /** Button text horizontal alignment label */
    public static final String BUTTON_HORIZONTAL_ALIGNMENT = "horizontalAlignment";
    /** Button text vertical alignment label */
    public static final String BUTTON_VERTICAL_ALIGNMENT = "verticalAlignment";
    /** Button icon type (size) label */
    public static final String BUTTON_ICON_TYPE = "iconType";
    /** Button have paint text label */
    public static final String BUTTON_WITH_TEXT = "withText";
    private static RolloverMouseListener rolloverListener = new RolloverMouseListener();

    /** Button icon type - size */
    private IconType iconType;
    /** If true button have paint text, else not */
    private boolean withText;
    /** Beauti button */
    private boolean areaFilled;
    /** Button text horizontal alignment */
    private int horizontalAlignment;
    /** Button text vertical alignment */
    private int verticalAlignment;
    
    /**
     *  Create new instance
     * @param iconType Button icon type (size).
     * @param withText If true button have paint text, else not.
     * @param areaFilled Beauti button.
     * @param horizontalAlignment Button text horizontal alignment.
     * @param verticalAlignment Button text vertical alignment
     * @param changes Property change listener
     */
    public ButtonProperties(IconType iconType, 
            boolean withText, boolean areaFilled,
            int horizontalAlignment, int verticalAlignment,
            PropertyChangeListener changes) {
        this.iconType = iconType;
        this.withText = withText;
        this.areaFilled = areaFilled;
        this.horizontalAlignment = horizontalAlignment;
        this.verticalAlignment = verticalAlignment;
        this.changes = changes;
    }

    /**
     *  Return button horizontal aligment
     * @return button horizontal aligment
     */
    public int getButtonHorizontalAlignment() {
        return horizontalAlignment;
    }

    public void setButtonHorizontalAlignment(int buttonHorizontalAlignment) {
        propertyChange(BUTTON_HORIZONTAL_ALIGNMENT,
                this.horizontalAlignment, buttonHorizontalAlignment);
        this.horizontalAlignment = buttonHorizontalAlignment;
    }

    /**
     *  Return button vertical aligment
     * @return button vertical aligment
     */
    public int getButtonVerticalAlignment() {
        return verticalAlignment;
    }

    public void setButtonVerticalAlignment(int buttonVerticalAlignment) {
        propertyChange(BUTTON_VERTICAL_ALIGNMENT,
                this.verticalAlignment, buttonVerticalAlignment);
        this.verticalAlignment = buttonVerticalAlignment;
    }

    /**
     *  Return icon type
     * @return
     */
    public IconType getIconType() {
        return iconType;
    }

    public void setIconType(IconType iconType) {
        propertyChange(BUTTON_ICON_TYPE, this.iconType, iconType);
        this.iconType = iconType;
    }

    /**
     *  Return true if button need to display text
     * @return true if button need to display text
     */
    public boolean isWithText() {
        return withText;
    }

    public void setWithText(boolean withText) {
        propertyChange(BUTTON_WITH_TEXT, this.withText, withText);
        this.withText = withText;
    }

    /**
     *  Return true if button have rollover mouse listener
     * @return true if button have rollover mouse listener
     */
    public boolean isAreaFilled() {
        return areaFilled;
    }

    public void setAreaFilled(boolean areaFilled) {
        propertyChange("button.areaFilled", this.areaFilled, areaFilled);
        this.areaFilled = areaFilled;
    }

    /**
     *  <b>Note:</b> should not be used to {@link javax.swing.JMenuItem}
     * @param button
     * @return
     */
    public AbstractButton makeButtonFilled(AbstractButton button){
        // new button if it`s null
        if (button == null){
            button = new JButton();
        }

        // set rollover listener
        if (areaFilled){
            rolloverListener.preInitButton(button);
            button.addMouseListener(rolloverListener);
        } else {
            button.removeMouseListener(rolloverListener);
            rolloverListener.postInitButton(button);
        }

        // set text aligment
        button.setHorizontalTextPosition(horizontalAlignment);
        button.setVerticalTextPosition(verticalAlignment);

        // set text 
        button.setHideActionText(isWithText());
        
        return button;
    }

    public static ButtonProperties getDefaultMenuButtonProperties(){
        return new ButtonProperties(IconType.small, true, false, RIGHT, CENTER, null);
    }

    public static ButtonProperties getDefaultPaneButtonProperties(){
        return new ButtonProperties(IconType.small, false, true, CENTER, CENTER, null);
    }

    public static ButtonProperties getDefaultToolbarButtonProperties(){
        return new ButtonProperties(IconType.medium, false, true, RIGHT, CENTER, null);
    }

    public static ButtonProperties getDefaultEditToolbarButtonProperties(){
        return new ButtonProperties(IconType.medium, true, true, CENTER, BOTTOM, null);
    }

    @Override
    public String toString() {
        return "ButtonProperties{" + "iconType=" + iconType
                + ";withText=" + withText + ";areaFilled=" + areaFilled
                + ";horizontalAlignment=" + horizontalAlignment
                + ";verticalAlignment=" + verticalAlignment + '}';
    }


}
